<template>
  <div class="reservation-container">
    <!-- 搜索表单 -->
    <el-form :inline="true" :model="searchForm" class="search-form">
      <el-form-item label="用户名：">
        <el-input v-model="searchForm.username" placeholder="请输入用户名" clearable />
      </el-form-item>
      <el-form-item label="手机号：">
        <el-input v-model="searchForm.phone" placeholder="请输入手机号" clearable />
      </el-form-item>
      <el-form-item label="预约状态：">
        <el-select v-model="searchForm.status" placeholder="请选择状态" clearable>
          <el-option label="待确认" value="pending" />
          <el-option label="已确认" value="confirmed" />
          <el-option label="已取消" value="canceled" />
        </el-select>
      </el-form-item>
      <el-form-item label="预约日期：">
        <el-date-picker
            v-model="searchForm.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">搜索</el-button>
        <el-button @click="resetSearch">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <div class="operation-buttons">
      <el-button type="primary" @click="handleAddReservation">
        <el-icon><Plus /></el-icon>新增预约
      </el-button>
      <el-button type="warning" @click="handleExport" :disabled="reservations.length === 0">
        <el-icon><Download /></el-icon>导出数据
      </el-button>
    </div>

    <!-- 预约用户列表 -->
    <el-table
        :data="reservations"
        style="width: 100%"
        v-loading="loading"
        border
        stripe
        highlight-current-row
        @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="username" label="用户名" width="120" />
      <el-table-column prop="phone" label="手机号" width="130" />
      <el-table-column prop="expectedDate" label="期望入住日期" width="150" />
      <el-table-column prop="budget" label="预算(元/月)" width="120">
        <template #default="{ row }">
          <span style="color: #e6a23c">{{ row.budget }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="houseType" label="房源类型" width="120">
        <template #default="{ row }">
          {{ getHouseTypeText(row.houseType) }}
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="100">
        <template #default="{ row }">
          <el-tag :type="getStatusTagType(row.status)">
            {{ getStatusText(row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="source" label="来源" width="100">
        <template #default="{ row }">
          {{ getSourceText(row.source) }}
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="160" />
      <el-table-column prop="remark" label="备注" show-overflow-tooltip />
      <el-table-column label="操作" width="180" fixed="right">
        <template #default="{ row }">
          <el-button size="small" @click="handleEdit(row)">编辑</el-button>
          <el-button
              size="small"
              type="danger"
              @click="handleCancel(row.id)"
              :disabled="row.status !== 'pending'"
          >
            取消
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页条 -->
    <div class="pagination-container">
      <el-pagination
          v-model:current-page="pageNum"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { Plus, Download } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 搜索表单
const searchForm = ref({
  username: '',
  phone: '',
  status: '',
  dateRange: []
})

// 分页数据
const pageNum = ref(1)
const pageSize = ref(10)
const total = ref(0)
const loading = ref(false)
const selectedRows = ref([])

// 表格数据
const reservations = ref([
  { id: 101, username: '孙八', phone: '13400134000', expectedDate: '2023-06-25', budget: 4000, houseType: 2, status: 'pending', source: 'website', createTime: '2023-06-20', remark: '希望带家具' },
  { id: 102, username: '周九', phone: '13300133000', expectedDate: '2023-06-26', budget: 3500, houseType: 1, status: 'confirmed', source: 'agent', createTime: '2023-06-19', remark: '需要近地铁' },
  { id: 103, username: '吴十', phone: '13200132000', expectedDate: '2023-06-28', budget: 5000, houseType: 3, status: 'confirmed', source: 'other', createTime: '2023-06-18', remark: '长期租住' },
  { id: 104, username: '郑十一', phone: '13100131000', expectedDate: '2023-06-30', budget: 3800, houseType: 2, status: 'canceled', source: 'app', createTime: '2023-06-21', remark: '计划变更' }
])

// 状态映射
const statusMap = {
  pending: { text: '待确认', tag: 'warning' },
  confirmed: { text: '已确认', tag: 'success' },
  canceled: { text: '已取消', tag: 'info' }
}

// 房源类型映射
const houseTypeMap = {
  1: '一居室',
  2: '两居室',
  3: '三居室',
  4: '别墅'
}

// 来源映射
const sourceMap = {
  website: '网站',
  app: 'APP',
  agent: '中介',
  other: '其他'
}

// 获取状态文本
const getStatusText = (status: string) => {
  return statusMap[status as keyof typeof statusMap]?.text || status
}

// 获取状态标签类型
const getStatusTagType = (status: string) => {
  return statusMap[status as keyof typeof statusMap]?.tag || ''
}

// 获取房源类型文本
const getHouseTypeText = (type: number) => {
  return houseTypeMap[type as keyof typeof houseTypeMap] || type
}

// 获取来源文本
const getSourceText = (source: string) => {
  return sourceMap[source as keyof typeof sourceMap] || source
}

// 初始化加载数据
onMounted(() => {
  total.value = reservations.value.length
  loading.value = false
})

// 搜索
const handleSearch = () => {
  pageNum.value = 1
  // 实际项目中应添加搜索过滤逻辑
  ElMessage.info('搜索功能已触发，实际项目中会根据条件过滤数据')
}

// 重置搜索
const resetSearch = () => {
  searchForm.value = {
    username: '',
    phone: '',
    status: '',
    dateRange: []
  }
  handleSearch()
}

// 分页大小改变
const handleSizeChange = (val: number) => {
  pageSize.value = val
  // 实际项目中应添加分页逻辑
  ElMessage.info(`分页大小已更改为${val}，实际项目中会加载对应数据`)
}

// 当前页改变
const handleCurrentChange = (val: number) => {
  pageNum.value = val
  // 实际项目中应添加分页逻辑
  ElMessage.info(`当前页已更改为${val}，实际项目中会加载对应数据`)
}

// 选择行变化
const handleSelectionChange = (rows: any[]) => {
  selectedRows.value = rows
}

// 新增预约
const handleAddReservation = () => {
  ElMessage.info('新增预约功能已触发，实际项目中会打开新增对话框')
}

// 导出数据
const handleExport = () => {
  console.log('导出选中的数据', selectedRows.value)
  ElMessage.success('导出功能需对接后端API')
}

// 编辑预约
const handleEdit = (row: any) => {
  ElMessage.info(`编辑预约: ${row.username}，实际项目中会打开编辑对话框`)
}

// 取消预约
const handleCancel = (id: string) => {
  ElMessageBox.confirm('确定要取消此预约吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    // 找到要取消的预约并更新状态
    const index = reservations.value.findIndex(item => item.id.toString() === id)
    if (index !== -1) {
      reservations.value[index].status = 'canceled'
      ElMessage.success('取消成功')
    } else {
      ElMessage.error('未找到该预约')
    }
  }).catch(() => {
    ElMessage.info('已取消操作')
  })
}
</script>

<style scoped lang="scss">
.reservation-container {
  padding: 20px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
}

.search-form {
  margin-bottom: 20px;
  :deep(.el-form-item) {
    margin-right: 15px;
    margin-bottom: 0;
  }
}

.operation-buttons {
  margin-bottom: 20px;
  display: flex;
  gap: 10px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.el-table {
  margin-top: 20px;
}
</style>